package com.itheima.Demo;

import com.itheima.dao.UserMapper;
import com.itheima.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Date;
import java.util.List;

public class demo {
    @Test//只是老版查询方法 不用了
    public void Test01() throws IOException {
        InputStream is = Resources.getResourceAsStream("mybatisConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        List<User> list = sqlSession.selectList("org.mybatis.example.BlogMapper.findAllUsers");
        list.forEach(l-> System.out.println(l.toString()));
        sqlSession.close();
    }


    @Test//动态代理方法 查询所有
    public void Test02() throws IOException {
        InputStream is = Resources.getResourceAsStream("mybatisConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List list = mapper.findall();
        list.forEach(l-> System.out.println(l.toString()));
        sqlSession.close();
    }

    @Test//动态代理方式添加
    public void Test03() throws IOException {
        InputStream is = Resources.getResourceAsStream("mybatisConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);//提交事务
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        Integer i = mapper.addUser(new User(0, "harry", new Date(0), "男", "武汉"));
        if (i>0){
            System.out.println("添加成功!");
        }
        sqlSession.close();
    }


    @Test//动态代理根据id修改
    public void Test04() throws IOException {
        InputStream is = Resources.getResourceAsStream("mybatisConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        Integer i = mapper.updateUser(new User(8, "Dale", new Date(0), "女", "天庭"));
        if (i>0){
            System.out.println("修改成功");
        }
        sqlSession.close();
    }















    @Test
    public void Test05() throws IOException {
        InputStream is = Resources.getResourceAsStream("mybatisConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        Integer i = mapper.deleteUser(8);
        if (i>0){
            System.out.println("删除成功");
        }
        sqlSession.close();
    }
}























